<?php
// Library to manipulate DOM objects similar to jquery
include_once 'simple_html_dom.php';

// Load HTML from a HTML file & Create a DOM object
$html = file_get_html($this->config->item("clients_url") . "$client_slug/templates/$tpl/$tpl.html");

// pw:line to span
foreach ($html->find('pw:line') as $element) {
  $element->tag   = 'span';
  $element->class = 'pw-line pw-component';
}

// pw:multiline to span
foreach ($html->find('pw:multiline') as $element) {
  $element->tag   = 'span';
  $element->class = 'pw-multiline pw-component';
}

// pw:unsubscribe to a
foreach ($html->find('pw:unsubscribe') as $element) {
  $element->tag = 'a';
  $element->href = base_url() . 'recipients/unsubscribe/.unsubscribe_key./';
}

// pw:read to img
foreach ($html->find('pw:read') as $element) {
  $element->tag = 'img';
  $element->src = base_url() . 'sends/read/.sends_key./'; 
}


// enclose img[editable=true] in span
foreach ($html->find('img[editable=true]') as $element) {
  $element->outertext =
    '<span class="pw-image pw-component">' .
       $element->outertext .
    '</span>';
}

// Backup html into string
$str_html_bkp = $html->root->innertext;

// Free memory
$html->clear();
unset($html);

// Create a DOM object and load backup
$html = new simple_html_dom();
$html->load($str_html_bkp);

// enclose a[editable=true] in span
foreach ($html->find('a[editable=true]') as $element) {
  $element->outertext =
    '<span class="pw-link pw-component">' .
       $element->outertext .
    '</span>';
}

// Backup html into string
$str_html_bkp = $html->root->innertext;

// Free memory
$html->clear();
unset($html);

// Create a DOM object and load backup
$html = new simple_html_dom();
$html->load($str_html_bkp);

// build blocks for pw:block[repeat=false]
foreach ($html->find('pw:block[repeat=false]') as $element) {
  $element->tag       = 'div';
  $element->class     = 'pw-block';
  $element->innertext =
		  '<div class="pw-block-content clearfix">'
			   . $element->innertext .
      '</div> <!-- pw-block-content -->
       <div class="pw-block-action btn-group">
         <a href="#" class="pw-action-edit btn" title="Editar"><i class="icon-pencil"></i></a>
       </div>';
}

// Backup html into string
$str_html_bkp = $html->root->innertext;

// Free memory
$html->clear();
unset($html);

// Create a DOM object and load backup
$html = new simple_html_dom();
$html->load($str_html_bkp);

//  pw:block[repeat=true] to div
foreach ($html->find('pw:block[repeat=true]') as $element) {
  $element->tag   = 'div';
  $element->class = 'pw-repeater pw-sortable';
}

//  building blocks pw:blocktype
$blocktype_count = 1;

foreach ($html->find('pw:blocktype') as $element) {
  $element->tag       = 'div';
  $element->class     = 'pw-blocktype';
  $element->id        = 'pw-blocktype-' . $blocktype_count;
  $element->innertext =
    '<div class="pw-block">
		   <div class="pw-block-content clearfix" rel="#' . $element->id . '">'
			   . $element->innertext .
      '</div> <!-- pw-block-content -->
       <div class="pw-block-action btn-group">
         <a href="#" class="pw-action-edit btn" title="Editar"><i class="icon-pencil"></i></a>
         <a href="#" class="pw-action-blocktype btn" rel="#' . $element->id . '" title="Duplicar"><i class="icon-file"></i></a>
         <a href="#" class="pw-action-remove btn" title="Eliminar"><i class="icon-trash"></i></a>
       </div>
		 </div><!-- pw-block -->';
  $blocktype_count++;
}

// Backup html into string
$str_html_bkp = $html->root->innertext;

// Free memory
$html->clear();
unset($html);

// Create a DOM object and load backup
$html = new simple_html_dom();
$html->load($str_html_bkp);

// clone blocktype as samples
foreach ($html->find('div.pw-blocktype') as $blocktype)
  for ($i=0; $i<$blocktype->clone; $i++)
    $blocktype->outertext = $blocktype->find('div.pw-block',0)->outertext . $blocktype->outertext;

// Backup html into string
$str_html_bkp = $html->root->innertext;

// Free memory
$html->clear();
unset($html);

// Create a DOM object and load backup
$html = new simple_html_dom();
$html->load($str_html_bkp);


// Set ids for .pw-component elements
$component_count = 1;

foreach ($html->find('.pw-component') as $element) {
  $element->id = 'pw-component-' . $component_count;
  $component_count++;
}

// Set ids for .pw-block-content elements
$block_content_count = 1;

foreach ($html->find('.pw-block-content') as $element) {
  $element->id = 'pw-block-content-' . $block_content_count;
  $block_content_count++;
}

// set rel attribute for .pw-block-action
foreach ($html->find('.pw-block') as $element)
  $element->find('.pw-block-action',0)->rel = '#' . $element->find('.pw-block-content',0)->id;

/*
 * inside blocktypes: delete id for .pw-component and .pw-block-content elements,
 * and delete rel for .pw-block-action
 */
foreach ($html->find('div.pw-blocktype') as $blocktype) {
  foreach ($blocktype->find('.pw-component') as $element)
    $element->id = null;
  
  foreach ($blocktype->find('.pw-block-content') as $element)
    $element->id = null;

  foreach ($blocktype->find('.pw-block-action') as $element)
    $element->rel = null;
}


/*
// Free memory
$html->clear();
unset($html);
*/